﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Interface;
using NetDon.OA.Interface.CRM;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;

namespace NetDon.OA.Portal.eLogisticsQuery
{
    [Navigation("报表查询", "开票查询")]
    public partial class InvoiceQueryPage : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                txtConfirmInvoiceDateStart.Text =
 DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");

                txtConfirmInvoiceDateEnd.Text =
   DateTime.Now.ToString("yyyy-MM-dd");
                ddlCompany.InsertEmptyItem(ListEmptyType.Select);
                this.ddlCustomer.InsertEmptyItem(ListEmptyType.Select);

                if (!this.IsSuperRoles())
                {
                    ddlCompany.SelectedValue = CurrentApplicationContext.CompanyId.ToString();
                    ddlCompany.Enabled = false;
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ObjectDataSourceCustomers.Selecting += (ds, dsArgs) =>
            {
                dsArgs.InputParameters["areaId"] = CurrentApplicationContext.CompanyId.ToString();
            };


        }

        public void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ddlCustomer.SelectedValue))
            {

               // LoadGoodsShippedTypes(ddlCustomer.SelectedValue.ToGuid());
            }


        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Session["InvoiceQuery"] = GetSearchCriteria();
            this.ScriptStartup("search();");

        }
        protected RevenueMachineAccountInvoiceSearchCriteria GetSearchCriteria()
        {
            var criteria = new RevenueMachineAccountInvoiceSearchCriteria();
            {

            };
            if (!MiscUtilities.IsNullOrEmpty(ddlCompany.SelectedValue))
            {
                criteria.AreaId = ddlCompany.SelectedValue.ToGuid();
            }

            DateTime? dateStart = null, dateEnd = null;
            if (!string.IsNullOrEmpty(txtConfirmInvoiceDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtConfirmInvoiceDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtConfirmInvoiceDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtConfirmInvoiceDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ConfirmInvoiceDate = new Range<DateTime?>(dateStart, dateEnd);
            }

            if (!MiscUtilities.IsNullOrEmpty(ddlRouteTypeId.SelectedValue.ToGuid()))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }
            if (!string.IsNullOrEmpty(ddlInvoiceType.SelectedValue))
            {
                criteria.InvoiceType = ddlInvoiceType.SelectedValue.ToGuid();
            }

            if (!MiscUtilities.IsNullOrEmpty(ddlCustomer.SelectedValue.ToGuid()))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
            decimal invoiceAmountStart, invoiceAmountEnd;
           
           if( decimal.TryParse(txtInvoiceAmountStart.Text, out invoiceAmountStart))
           {
               criteria.InvoiceAmountStart = invoiceAmountStart;
           }
           if (decimal.TryParse(txtInvoiceAmountEnd.Text, out invoiceAmountEnd))
           {
               criteria.InvoiceAmountEnd = invoiceAmountEnd;
           }
            return criteria;


        }

    }
}